package com.pda.common_easy.pages.pro;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.common_easy.beanin.DownBillBeanIn;
import com.pda.common_easy.beanout.DownBillBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class ProBillModelImpl extends BaseModel implements ProBillModel {
    private String TAG = "ProBillModelImpl";
    private DownBillBeanOut downBillBeanOut;

    public ProBillModelImpl(Context context) {
        super(context);
    }

    @Override
    public void showlist(final String bill,final CallBack callBack) {
        try {
            DownBillBeanIn downBillBeanIn = new DownBillBeanIn();
            downBillBeanIn.setAction("DownloadBill_Detail");
            downBillBeanIn.setCid(SharedPreferencesManger.getCid());
            downBillBeanIn.setUserID(SharedPreferencesManger.getUid());
            downBillBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            downBillBeanIn.setBillCode(bill);
            downBillBeanIn.setBillType("11");
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(downBillBeanIn));
            api.login(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.onshowp(downBillBeanOut.getData());
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (downBillBeanOut != null && downBillBeanOut.getErrmsg() != null) {
                                callBack.ontip(downBillBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            downBillBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), DownBillBeanOut.class);
                            if (downBillBeanOut != null && downBillBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
}

